package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDto dto) {
        boolean islikes=false,isunlikes=false,iscollection=false,isfollow=false;
        ApUser user = AppThreadLocalUtils.getUser();
        if(user!=null) {
            ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
      if(behaviorEntry==null){
          CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST,"未查询到行为实体信息");
      }
            Query query = Query.query(Criteria.where("entryId").is(behaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
            ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if(likesBehavior!=null){
                islikes=true;
            }
            ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
            if(unlikesBehavior!=null){
                isunlikes=true;
            }
            ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
            if(apCollection!=null){
                isfollow=true;
            }

            Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + user.getId(), dto.getAuthorId().toString());
            if(score!=null){
                isfollow =true;
            }
        }
        Map map = new HashMap();
        map.put("islikes",islikes);
        map.put("iscollection",iscollection);
        map.put("isunlikes",isunlikes);
        map.put("isfollow",isfollow);
        return ResponseResult.okResult(map);
    }
}
